package org.tankwar.model;

import org.tankwar.type.Direction;
import org.tankwar.util.GlobalUtil;
import org.tankwar.util.ImageUtil;
import org.tankwar.view.GamePanel;

import java.awt.*;

public class EnemyTank extends Tank{
    public static int count = 0;
    public EnemyTank(int x, int y, Direction dir, GamePanel tc) {// EnemyTank的构造函数
        tankImages = new Image[] { GlobalUtil.tk.getImage(this.getClass().getResource(ImageUtil.TANK_DOWN_IMAGE_URL)),
                GlobalUtil.tk.getImage(this.getClass().getResource(ImageUtil.TANK_UP_IMAGE_URL)),
                GlobalUtil.tk.getImage(this.getClass().getResource(ImageUtil.TANK_LEFT_IMAGE_URL)),
                GlobalUtil.tk.getImage(this.getClass().getResource(ImageUtil.TANK_RIGHT_IMAGE_URL)), };
        this.x = x;
        this.y = y;
        this.oldX = x;
        this.oldY = y;
        this.direction = dir;
        this.tc = tc;
        this.good = false;
        this.life = 100;
    }

    @Override
    public void draw(Graphics g) {
        if (!live) {
            tc.tanks.remove(this); // 删除无效的
        }
        drawTank(g);
        move();
    }

    /**
     * 敌军坦克的移动方法，自动移动
     */
    @Override
    public void move() {
        baseMove();
        // 自动移动
        Direction[] directions = Direction.values();
        if (step == 0) {
            step = GlobalUtil.rand.nextInt(12) + 3; // 产生随机路径
            int rn = GlobalUtil.rand.nextInt(directions.length);
            status = directions[rn]; // 产生随机方向
        }
        step--;
        if (GlobalUtil.rand.nextInt(40) > 38)// 产生随机数，控制敌人开火
            this.fire();
    }
}
